package com.usfuture.ins.view
{
	import com.usfuture.Canvas;
	import com.usfuture.ICallRequest;
	import com.usfuture.ICanvas;
	import com.usfuture.chart.ins.InsChart;
	import com.usfuture.event.BusyMaskEvent;
	import com.usfuture.event.DataEvent;
	import com.usfuture.ins.entity.Ins;
	import com.usfuture.ins.proxy.InsProxy;
	import com.usfuture.insInfo.entity.InsInfo;
	import com.usfuture.techanly.view.Data;
	import com.usfuture.util.DateTimeUtils;
	import com.usfuture.util.NumberFormat;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	public class InsContainer extends Sprite implements ICallRequest
	{
		//最新走势显示
		private var _lastInsShowLabel:TextField;
		private var _lastInsTimeLabel:TextField;
		
		private var _insCanvas:Canvas;
		private var _insChart:InsChart;
		private var _insShowLabel:TextField;//当前鼠标所在位置的走势信息
		
		
		private var _insProxy:InsProxy;
		private var _insData:Array;
		private var _insInfo:InsInfo;
		private var isInsDataResponse:Boolean=true;
		private var isInsInfoDataResponse:Boolean=true;
		private var _initRequestStatus:uint=0;//初始请求状态 0:未请求，1:正在请求, 2:请求结束
		
		public function InsContainer(width:Number,height:Number)
		{
			super();
			
			//添加走势鼠标所在位置信息label
			_insShowLabel = new TextField;
			_insShowLabel.selectable = false;
			_insShowLabel.x = 60;
			_insShowLabel.y = 0;
			_insShowLabel.width = 400;
			_insShowLabel.htmlText="<font color='#000000'>━ 成交價</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='#CCCC00'>━ 均價</font>"
			this.addChild(_insShowLabel);
			
			//添加最后一筆時間顯示label
			_lastInsTimeLabel = new TextField;
			_lastInsTimeLabel.selectable = false;
			_lastInsTimeLabel.x = width-100-180  - 60;
			_lastInsTimeLabel.y = 0;
			_lastInsTimeLabel.width = 180;
			this.addChild(_lastInsTimeLabel);
			
			//添加走势面板 
			_insCanvas = new Canvas(width - 100,height - 20);
			_insCanvas.x = 5;
			_insCanvas.y = 12;
			this.addChild(_insCanvas);
			
			//载入走势画板
			_insChart = new InsChart(_insCanvas,future.startTime,future.endTime,future.minuteShow,future.hourOffset,future.restHour);
			_insChart.addEventListener(DataEvent.CURRENT_MOUSE_POSITION_DATA,currentMousePositiondataHandler);
			
			_insInfo = new InsInfo;
			//添加走势显示label
			_lastInsShowLabel = new TextField;
			_lastInsShowLabel.multiline = true;
			_lastInsShowLabel.selectable = false;
			_lastInsShowLabel.x = width - 100;
			_lastInsShowLabel.y = 0;
			_lastInsShowLabel.width = 180;
			_lastInsShowLabel.height = 300;
			this.addChild(_lastInsShowLabel);
			setInsInfoShowText(_insInfo);
			 
//			//添加监听
//			this.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
			
			
			_insChart.insInfo = _insInfo;
			_insChart.data = _insData;
			ExternalInterface.addCallback("responseInsInfo",responseInsInfo);
			ExternalInterface.addCallback("responseIns",responseIns);
		}
		
		
		public function initCall():void{
			liveCall();
		}
		
		public function liveCall():void{
			//如果是第一次请求
			if(initRequestStatus == 0)
				requestInsInfo();
			else{
				requestInsInfo();
				
			}
		}
		
		public function requestInsInfo():void{
			if(initRequestStatus == 0) initRequestStatus ++;
			if(isInsInfoDataResponse){
				isInsInfoDataResponse = false;
				ExternalInterface.call("requestInsInfo_ins");
			}
		}
		
		public function requestIns():void{
			if(isInsDataResponse){
				isInsDataResponse = false;
				var lastTimestamp:String;
				if(_insData&&_insData.length>0)
					lastTimestamp = DateTimeUtils.date2Str(Ins(_insData[_insData.length-1]).timestamp);
				ExternalInterface.call("requestIns",lastTimestamp);
			}
		}
		
		/**
		 * 返回即时信息数据
		 * @param map
		 */
		public function responseInsInfo(map:Object):void{
			isInsInfoDataResponse = true;
			if(!map) return;
			requestIns();
			_insInfo.timestamp = DateTimeUtils.str2Date(map.timestamp);
			_insInfo.ycPrice = map.ycPrice;
			_insInfo.openPrice = map.openPrice;
			_insInfo.newPrice = map.newPrice;
			_insInfo.highPrice = map.highPrice;
			_insInfo.lowPrice = map.lowPrice;
			_insInfo.allVolume = map.amount;
			setInsInfoShowText(_insInfo);
		}
		
		/**
		 * 返回即时数据
		 * @param map
		 */
		public function responseIns(data:Array):void{
			if(initRequestStatus == 1) initRequestStatus ++;
			if(!data || data.length == 0){
				isInsDataResponse = true;
				return;
			}
			var insList:Array = new Array;
			for each(var obj:Object in data){
				var ins:Ins = new Ins;
				ins.timestamp = DateTimeUtils.str2Date(obj.timestamp);
				ins.newPrice = parseFloat(obj.closePrice);
				ins.allVolume = parseFloat(obj.amount);
				insList.push(ins);
			}
			
			if(!_insData||_insData.length==0){
				_insData = insList;
			}else{
				_insData.pop();
				for each(var ins:Ins in insList){
					_insData.push(ins);
				}
			}
			//处理数据
			var allPrice:Number=0;
			for(var i:uint=0;i<_insData.length;i++){
				var _ins:Ins = _insData[i] as Ins;
				_ins.rise = NumberFormat.twoPrecision(_ins.newPrice-_insInfo.ycPrice);
				_ins.risePer = NumberFormat.twoPrecision((_ins.newPrice-_insInfo.ycPrice)/_insInfo.ycPrice*100);
				allPrice+=Number(_ins.newPrice);
				_ins.allPrice=allPrice;
				_ins.avePrice=allPrice/(i+1);
			}
			_insChart.data = _insData;
			
			isInsDataResponse = true;
		}
//		
//		private function mouseMoveHandler(event:MouseEvent):void{
//			_insChart.dispatchEvent(event);
//		}
		
		/**
		 * 当前鼠标位置数据
		 * @param event
		 */
		private function currentMousePositiondataHandler(event:DataEvent):void{
			switch(event.target){
				case _insChart:
					var ins:Ins = event.data.ins as Ins;
					var htmlTxt:String = '<font size="14"><font color="#af8c52">時間:'+(ins.timestamp.getHours()<10?('0'+ins.timestamp.getHours()):ins.timestamp.getHours())+':'+(ins.timestamp.getMinutes()<10?('0'+ins.timestamp.getMinutes()):ins.timestamp.getMinutes())+'</font>  ';
					htmlTxt+='<font color="#af8c52">最新:</font><font color="'+getColor(ins.newPrice,_insInfo.ycPrice)+'">'+NumberFormat.twoPrecisionStr(ins.newPrice)+'</font>    ';
					htmlTxt+='<font color="#af8c52">均價:</font><font color="'+getColor(ins.avePrice,_insInfo.ycPrice)+'">'+NumberFormat.twoPrecisionStr(ins.avePrice)+'</font>    ';
					htmlTxt+='<font color="#af8c52">成交量:'+int(ins.allVolume)+'</font></font>';
					_insShowLabel.htmlText = htmlTxt;
					break;
				default:break;
			}
		}
		
		private function setInsInfoShowText(insInfo:InsInfo):void{
			var htmlTxt:String='<font size="14">';
			htmlTxt+='<B><font size="24" color="'+getColor(insInfo.newPrice,insInfo.ycPrice)+'">'+num2Str(insInfo.newPrice)+'</font><font size="12" color="'+getColor(insInfo.newPrice,insInfo.ycPrice)+'">'+' '+((insInfo.newPrice>=insInfo.ycPrice)?'△':'▽')+Math.round(insInfo.rise)+'</font></B><br>';
//			htmlTxt+='<font color="#d68d48">委買價:</font><font color="#902131">8744</font><br>';
//			htmlTxt+='<font color="#d68d48">委賣價:</font><font color="#902131">8745</font><br>';
//			htmlTxt+='<font color="#938463">漲    跌:</font><font color="'+getColor(insInfo.newPrice,insInfo.ycPrice)+'">'+NumberFormat.twoPrecisionStr(insInfo.rise)+'</font><br>';
			htmlTxt+='<font color="#d68d48">漲跌幅: </font><font color="'+getColor(insInfo.newPrice,insInfo.ycPrice)+'">'+NumberFormat.twoPrecisionStr(insInfo.risePer)+'%</font><br>';
			htmlTxt+='<font color="#d68d48">昨    收: </font><font color="#a2a091">'+num2Str(insInfo.ycPrice)+'</font><br>';
			htmlTxt+='<font color="#d68d48">開    盤: </font><font color="'+getColor(insInfo.openPrice,insInfo.ycPrice)+'">'+num2Str(insInfo.openPrice)+'</font><br>';
			htmlTxt+='<font color="#d68d48">最    高: </font><font color="'+getColor(insInfo.highPrice,insInfo.ycPrice)+'">'+num2Str(insInfo.highPrice)+'</font><br>';
			htmlTxt+='<font color="#d68d48">最    低: </font><font color="'+getColor(insInfo.lowPrice,insInfo.ycPrice)+'">'+num2Str(insInfo.lowPrice)+'</font><br>';
			htmlTxt+='<font color="#d68d48">總    量: </font><font color="#af8c52">'+num2Str(insInfo.allVolume)+'</font><br>';
			htmlTxt+='</font>';
			_lastInsShowLabel.htmlText= htmlTxt;
			if(insInfo.timestamp)
				_lastInsTimeLabel.htmlText = "<font color='#909090'>成交時間："+DateTimeUtils.date2Str(insInfo.timestamp,1)+"</font>"
		}
		
		/**
		 * 根据价格与昨收价返回颜色
		 * @param price
		 * @param ycPrice
		 * @return 
		 */
		private function getColor(price:Number,ycPrice:Number):String{
			return price>ycPrice?'#c61b13':(price==ycPrice)?'#a2a091':'#2c6822'
		}
		
		/**
		 * 根据价格返回指定的格式
		 * @param price
		 * @return 
		 */
		private function num2Str(num:Number):String{
			return isNaN(num)||num==0?'----':num+'';
		}

		
		public function get initRequestStatus():uint
		{
			return _initRequestStatus;
		}
		
		public function set initRequestStatus(value:uint):void
		{
			_initRequestStatus = value;
			if(_initRequestStatus==1)
				dispatchEvent(new BusyMaskEvent(BusyMaskEvent.SHOW));
			if(_initRequestStatus==2)
				dispatchEvent(new BusyMaskEvent(BusyMaskEvent.HIDE));
		}

		
	}
}